package com.annhoa.domain.service.es;

import com.annhoa.domain.es.Book;
import com.annhoa.domain.repository.es.ESBookRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author geng
 * 2020/12/19
 */
@Slf4j
@Service
public class BookService {

    @Autowired
    ESBookRepository esBookRepository;

    public List<Book> searchBook(String keyword){
        return esBookRepository.findByTitleOrAuthor(keyword, keyword);
    }

    public Book addBook(Book book) {
        try {
            return esBookRepository.save(book);
        }catch (Exception e){
            log.error(String.format("保存ES错误！%s", e.getMessage()));
            throw new RuntimeException(e.getMessage());
        }
    }

    public SearchHits<Book> searchBook1(String keyword){
        return esBookRepository.find(keyword);
    }
}
